package com.securecrt;

import java.net.URL;

import org.xmlpull.v1.XmlPullParser;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AutoUpdateNewVersion extends Activity {
	public static String URL_LIST_PRODUCT = "http://14.63.212.204/eli-android-center/data/auto-update/listitem.xml";
	ListView list_product;
	Context context;
	AppAdapter appAdapter;
	ProgressDialog progress;
	Double newVersionName=0.0;
	Double currentVersionName=0.0;
	String curDescription="",curpkg="",cursize="",curlink="";
	Double curversionCode=0.0;
	private Handler mHandler = new Handler();
	TextView verson; 
	ProgressDialog pro;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autoupdatenewversion);
		CheckNewVersion();
		verson = (TextView)findViewById(R.id.tvVersion);
		verson.setText("Verson "+getSoftwareVersion());
		
	}
	
	private void CheckNewVersion(){
		getProductname();
		currentVersionName=Double.parseDouble(getSoftwareVersion());
		if(newVersionName>currentVersionName && getProductname().equalsIgnoreCase(getResources().getString(R.string.app_name))){
        	showDialogUpdateNewVersion();
		}
		else
			mHandler.postDelayed(passContext, 3000);
	}
	
	private String getSoftwareVersion(){
		String vsname="";
		try{
			PackageInfo pinfo=getPackageManager().getPackageInfo(getPackageName(), 0);
			vsname= pinfo.versionName;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return vsname;
	}
	

	private void showDialogUpdateNewVersion(){
    	AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
    	alertDialog.setTitle("Confirm update new version...");
    	alertDialog.setMessage("Do you want to update this new version?");
//    	alertDialog.setIcon(R.drawable.)
    	alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				loadList();
			}
		});
    	alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				mHandler.postDelayed(passContext, 3000);
			}
		});
    	alertDialog.show();
    }

	private Runnable passContext = new Runnable() {
		   public void run() {
		       // do what you need to do here after the delay
			   passContext();
		   }
	};
	private void passContext() {
		// TODO Auto-generated method stub
		Intent i=new Intent(this,ssh2.class);
		startActivity(i);
	}
	private void loadList() {
		// TODO Auto-generated method stub
//		processing(this);
		String[] item = { getProductname(),"",curDescription,curlink,cursize,newVersionName.toString()};
		Intent intent=new Intent(this, AppDetail.class);
		intent.putExtra("item", item);
		intent.putExtra("pkg", curpkg);
		intent.putExtra("verCode", curversionCode);
		startActivity(intent);
	}
	

	private String getProductname(){
		String nameCurrent="";
		try{
			XmlPullParser xmlPullParser = Xml.newPullParser();
			URL url = new URL(URL_LIST_PRODUCT);
			xmlPullParser.setInput(url.openStream(), "UTF-8");
			int eventType;
			Double versionCode=0.0;
			String name="",version="",description="",pkg="",size="",link="";
			while ((eventType = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xmlPullParser.getName().equals("name")) {
						name = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){
							nameCurrent=name;
						}
					}else if (xmlPullParser.getName().equals("package")) {
						pkg = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){
							curpkg=pkg;
						}
					} else if (xmlPullParser.getName().equals("size")) {
						size = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){
							cursize=size;
						}
					}else if (xmlPullParser.getName().equals("version")) {
						version = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){
							versionCode=Double.parseDouble(version);
							curversionCode=versionCode;
							if(newVersionName<versionCode)
								newVersionName=versionCode;
						}
					}else if (xmlPullParser.getName().equals("link")) {
						link = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){curlink=link;}
					}else if (xmlPullParser.getName().equals("description")) {
						description = xmlPullParser.nextText();
						if(name.equalsIgnoreCase(getResources().getString(R.string.app_name))){
							curDescription=description;
						}
					}
				}
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return nameCurrent;
	}
	

	
}

